/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.KosDAO;
import com.ivanbiz.dao.impl.KosDAOImpl;
import com.ivanbiz.model.Kos;
import com.ivanbiz.service.ServiceHelper;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class KosPanel extends javax.swing.JPanel {

    List<Kos> listKos;
    Kos kosKamar;
    JDialog jDialogKamar;
    String kamar;

    public Kos getKosKamar() {
        return kosKamar;
    }

    public void setKosKamar(Kos kosKamar) {
        this.kosKamar = kosKamar;
    }

    public KosPanel() {
        initComponents();
        refresh();
    }

    public KosPanel(JDialog jDialogKos, String kamar) {
        initComponents();
        this.jDialogKamar = jDialogKos;
        this.kamar = kamar;
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableKos = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        buttonTambah = new javax.swing.JButton();
        buttonUbah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Daftar Kos");

        tableKos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableKos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableKosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableKos);

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah Kos Baru");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });
        jPanel1.add(buttonTambah);

        buttonUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/ubah.jpg"))); // NOI18N
        buttonUbah.setText("Ubah Kos Terseleksi");
        buttonUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUbahActionPerformed(evt);
            }
        });
        jPanel1.add(buttonUbah);

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Hapus Kos Terseleksi");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });
        jPanel1.add(buttonHapus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tableKosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableKosMouseClicked
        if (kamar != null) {
            if (evt.getClickCount() == 2) {
                kosKamar = listKos.get(tableKos.getSelectedRow());
                jDialogKamar.dispose();
            }
        }
    }//GEN-LAST:event_tableKosMouseClicked

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        JDialog jDialog = new JDialog();
        jDialog.setModal(true);
        jDialog.setSize(375, 375);
        jDialog.add(new KosUpdatePanel(jDialog));
        jDialog.setLocationRelativeTo(null);
        jDialog.setVisible(true);
        refresh();
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUbahActionPerformed
        if (tableKos.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan diubah", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            Kos kos = listKos.get(tableKos.getSelectedRow());
            JDialog jDialog = new JDialog();
            jDialog.setModal(true);
            jDialog.setSize(375, 375);
            jDialog.add(new KosUpdatePanel(jDialog, kos));
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
            refresh();
        }
    }//GEN-LAST:event_buttonUbahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tableKos.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan diubah", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Kos kos = listKos.get(tableKos.getSelectedRow());
                KosDAO kosDAO = new KosDAOImpl();
                kosDAO.delete(kos);
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(KosPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonHapusActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JButton buttonUbah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableKos;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            KosDAO kosDAO = new KosDAOImpl();
            listKos = kosDAO.getAll(Kos.class);
            updateTableKos();
        } catch (Exception ex) {
            Logger.getLogger(KosPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableKos() {
        String[] judul = {"No", "Kode Kos", "Nama", "Telp/HP", "Alamat"};
        Object[][] isi = new Object[listKos.size()][5];
        int x = 0;
        int no = 0;
        for (Kos kos : listKos) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = kos.getKodeKos();
            isi[x][2] = kos.getNama();
            isi[x][3] = kos.getTelp();
            isi[x][4] = kos.getAlamat();
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableKos);
    }
}
